#!/bin/sh

alias curl="curl -stderr"

if [[ $# -eq 2 ]];then #historical data

_month=`echo $2 |cut -d'/' -f1`
month=`echo "$_month - 1"|bc`
day=`echo $2 |cut -d'/' -f2`
year=`echo $2 |cut -d'/' -f3`

curl -stderr "http://finance.yahoo.com/q/hp?s=$1&a=$month&b=$day&c=$year&d=$month&e=$day&f=$year&g=d" |sed 's/tr/\n/g' |egrep -o "[0-9]+-[A-Za-z]+-[0-9]+|[0-9]+.[0-9]+</td></|>[0-9,]+<"  |tr '<\n' ' '|sed 's/td/\n/g' |sed -e 's/>//g' -e 's/\///g' | sed 's/,//g'| awk '{print $1" "$3" "$2}' |head -n 1 |awk '{print $2}'

else #current date
#echo "Symbol,Name,Previous Close,Open,Last Trade,Change%"
result=`curl "http://download.finance.yahoo.com/d/quotes.csv?s=$1&f=sl1p2cn"`
ticker=`echo $result |cut -d, -f1|sed 's/"//g'`
stream=`echo $result |cut -d, -f2|sed 's/"//g'`
change=`echo $result |cut -d, -f4|sed 's/"//g'`

export industryquery=`curl "http://finance.yahoo.com/q/in?s=$1" |sed 's/\<tr\>/\n/g' |egrep -B 1 -A 1 "\^YHO[a-zA-Z][0-9]{1,4}"`
export industry=`echo $industryquery |egrep -o ">[A-Z][a-z].*[A-Za-z]<"|sed 's/<//g' |sed 's/>//g' |sed 's/,//g'`
export industryupdown=`echo $industryquery |egrep  -o "Down|Up" |sed 's/Down/-/g' |sed 's/Up/+/g'`
export industrypercent=`echo $industryquery |egrep -o ">[0-9]+.[0-9]+%" |sed 's/>//g'`

#echo $change  
echo $ticker:$stream $change Industry:$industryupdown $industrypercent
fi

